package vn.axonactive.devday.jsf.session4;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import vn.axonactive.devday.jsf.model.StockQuote;
import vn.axonactive.devday.jsf.model.stockfeed.StockQuoteFactory;

/**
 * A JSF {@link ManagedBean} instance to interact with StockQuote board. Initially it provides a list of fake {@link StockQuote} which is loaded from configured data. 
 * It also supports {@link #addNewStockQuote(StockQuote)} method which can be used to add more {@link StockQuote} or {@link #removeStockQuote(StockQuote)} method 
 * which can be used to remove an existing {@link StockQuote}. 
 */
@ManagedBean
@SessionScoped
public class StockBoardBean implements Serializable{
	
	private static final long serialVersionUID = 3492733269781577833L;

	private List<StockQuote> stockQuoteList;
	
	public StockBoardBean()
	{
		try 
		{
			stockQuoteList = StockQuoteFactory.getInstance().feedData();
		} catch (IOException e) {
			// add log here
			throw new IllegalStateException(e);
		}
	}

	public List<StockQuote> getStockQuoteList()
	{
		return stockQuoteList;
	}

	public void setStockQuoteList(List<StockQuote> stockQuoteList)
	{
		this.stockQuoteList = stockQuoteList;
	}
	
	public void addNewStockQuote(StockQuote stockQuote)
	{
		//check if duplicated symbol in need
		stockQuoteList.add(stockQuote);
	}
	
	public void removeStockQuote(StockQuote stockQuote)
	{
		stockQuoteList.remove(stockQuote);
	}
	
}
